package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.common.ReggieConstants;
import com.reggie.entity.Employee;
import com.reggie.exception.CustomException;
import com.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        if (StringUtils.isBlank(employee.getPassword()))
            return R.error("密码为空");
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(lambdaQueryWrapper);
        if (one == null)
            return R.error("用户名不存在");
        String s = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!one.getPassword().equals(s))
            return R.error("密码错误");
        if (one.getStatus() == 0)
            return R.error("账号被禁用");
        request.getSession().setAttribute(ReggieConstants.EMP_SESSION_KEY, one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute(ReggieConstants.EMP_SESSION_KEY);
        return R.success("登出成功");
    }

    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name) {
        Page<Employee> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
        //排序
        objectLambdaQueryWrapper.orderByDesc(Employee::getCreateTime);
        employeeService.page(page1, objectLambdaQueryWrapper);
        return R.success(page1);
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex(ReggieConstants.DEFAULT_PWD.getBytes()));
/*        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute(ReggieConstants.EMP_SESSION_KEY);
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee) {
/*        Long id = (Long) request.getSession().getAttribute((ReggieConstants.EMP_SESSION_KEY));
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now());*/
        Employee byId = employeeService.getById(employee.getId());
        if (byId.getUsername().equals(ReggieConstants.ADMIN_KEY) && employee.getStatus().equals(0))
            throw new CustomException("管理员账号不能被禁用");
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
